package com.quanshu.shop.main.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.bumptech.glide.Glide;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.goods.view.SpecialGoodsActivity;
import com.quanshu.shop.main.entity.ShopMainEntity;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * 作者  李哲
 * 创建时间 2018/7/17
 * 邮箱 lizhetongliao@163.com
 * 说明
 **/
public class BannerAdapter extends DelegateAdapter.Adapter<BannerAdapter.BannerViewHolder> {
    private Context mContext;
    private List<ShopMainEntity.BannerEntity> imageUrlList;
    private LayoutHelper layoutHelper;
    private int current = 0;
    public static final int BANNER_TYPE = 0;

    public BannerAdapter(Context mContext, List<ShopMainEntity.BannerEntity> imageUrlList, LayoutHelper layoutHelper) {
        this.mContext = mContext;
        this.imageUrlList = imageUrlList;
        this.layoutHelper = layoutHelper;

    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return layoutHelper;
    }

    @Override
    public BannerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BannerViewHolder bannerViewHolder;
        if (viewType == BANNER_TYPE) {
            bannerViewHolder = new BannerViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_shop_main_banner, parent, false));
            return bannerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(BannerViewHolder holder, int position) {
        holder.bgaBanner.setAdapter(new BGABanner.Adapter<ImageView, ShopMainEntity.BannerEntity>() {
            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, ShopMainEntity.BannerEntity model, int position) {
                Glide.with(mContext).load(Config.URL_HOST_SHOP + model.getMob_headpic()).into(itemView);
            }
        });
        holder.bgaBanner.setData(imageUrlList, new ArrayList<>());
        holder.bgaBanner.setCurrentItem(current);
        holder.bgaBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d("首页轮播选择", "onPageSelected: " + position);
                current = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        holder.bgaBanner.setDelegate(new BGABanner.Delegate() {
            @Override
            public void onBannerItemClick(BGABanner banner, View itemView, @Nullable Object model, int position) {
                Log.d("首页轮播点击", "onClick " + position);
                Bundle bundle = new Bundle();
                bundle.putString(SpecialGoodsActivity.SPECIAL_ID, imageUrlList.get(position).getId());
                BaseTool.goActivity(mContext, SpecialGoodsActivity.class, bundle);
            }
        });
        holder.bgaBanner.setAutoPlayAble(true);
    }

    @Override
    public int getItemViewType(int position) {
        return BANNER_TYPE;
    }

    @Override
    public int getItemCount() {
        return imageUrlList != null ? 1 : 0;
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        BGABanner bgaBanner;

        BannerViewHolder(View itemView) {
            super(itemView);
            bgaBanner = itemView.findViewById(R.id.shop_main_banner);
        }
    }
}
